<template>
  <div class="register-container">
    <div class="register-card">
      <div class="register-header">
        <h2>医生注册</h2>
        <p>请填写以下信息完成注册</p>
      </div>

      <el-form
        ref="formRef"
        :model="form"
        :rules="formRules"
        label-width="100px"
        class="register-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" clearable />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password clearable />
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="form.confirmPassword" type="password" placeholder="请再次输入密码" show-password clearable />
        </el-form-item>

        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名" clearable />
        </el-form-item>

        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender" placeholder="请选择性别" style="width: 100%">
            <el-option label="男" :value="1" />
            <el-option label="女" :value="2" />
          </el-select>
        </el-form-item>

        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" clearable />
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" clearable />
        </el-form-item>

        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" clearable />
        </el-form-item>

        <el-form-item label="执业范围" prop="practiceScope">
          <el-input v-model="form.practiceScope" placeholder="请输入执业范围" clearable />
        </el-form-item>

        <el-form-item label="所属医院" prop="hospitalName">
          <el-select v-model="form.hospitalName" placeholder="请选择所属医院" style="width: 100%" :loading="hospitalLoading">
            <el-option v-for="hospital in hospitalList" :key="hospital" :label="hospital" :value="hospital" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleSubmit" :loading="submitLoading" style="width: 100%">
            注册
          </el-button>
        </el-form-item>

        <el-form-item>
          <el-button @click="goToLogin" style="width: 100%">
            已有账号？去登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { doctorRegister, getHospitals } from '../api/doctor'

const router = useRouter()

// 响应式数据
const formRef = ref()
const submitLoading = ref(false)
const hospitalLoading = ref(false)
const hospitalList = ref([])

// 表单数据
const form = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  realName: '',
  gender: 1,
  phone: '',
  email: '',
  address: '',
  practiceScope: '',
  hospitalName: ''
})

// 表单验证规则
const formRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在3到20个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在6到20个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== form.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  realName: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' }
  ],
  gender: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ],
  address: [
    { required: true, message: '请输入地址', trigger: 'blur' }
  ],
  practiceScope: [
    { required: true, message: '请输入执业范围', trigger: 'blur' }
  ],
  hospitalName: [
    { required: true, message: '请选择所属医院', trigger: 'change' }
  ]
}

// 获取医院列表
const fetchHospitalList = async () => {
  try {
    hospitalLoading.value = true
    const response = await getHospitals()
    if (response.code === 200) {
      hospitalList.value = response.data.hospitals || []
    } else {
      ElMessage.error(response.message || '获取医院列表失败')
    }
  } catch (error) {
    console.error('获取医院列表失败:', error)
    ElMessage.error('获取医院列表失败，请检查网络连接')
  } finally {
    hospitalLoading.value = false
  }
}

// 提交注册
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
    submitLoading.value = true
    
    const response = await doctorRegister(form)
    
    if (response.code === 200) {
      ElMessage.success('注册成功！')
      localStorage.setItem('doctorToken', response.data.token)
      localStorage.setItem('doctorInfo', JSON.stringify(response.data.doctor))
      router.push('/doctor-dashboard')
    } else {
      ElMessage.error(response.message || '注册失败')
    }
  } catch (error) {
    console.error('注册失败:', error)
    ElMessage.error(error.response?.data?.message || '注册失败，请检查网络连接')
  } finally {
    submitLoading.value = false
  }
}

// 跳转到登录页面
const goToLogin = () => {
  router.push('/login')
}

// 生命周期
onMounted(() => {
  fetchHospitalList()
})
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.register-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 500px;
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
}

.register-header h2 {
  color: #333;
  margin: 0 0 10px 0;
  font-size: 28px;
  font-weight: 600;
}

.register-header p {
  color: #666;
  margin: 0;
  font-size: 14px;
}

.register-form {
  margin-top: 20px;
}

.el-form-item {
  margin-bottom: 20px;
}

.el-button {
  height: 40px;
  font-size: 16px;
  border-radius: 6px;
}

.el-button--primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
}

.el-button--primary:hover {
  background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
}
</style> 